﻿namespace GuiaTest
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for TreeViewTest
    /// </summary>
    [TestClass]
    public class TreeViewTest : AbstractWindowTest
    {
        [TestMethod]
        public void TestGetItemsIntIndexer()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            // check the visible items
            Assert.AreEqual("Item 1", treeView1[0].Header);
            Assert.AreEqual("Item 2", treeView1[1].Header);
            Assert.AreEqual("Item 3", treeView1[2].Header);
            // check a subitem
            Assert.AreEqual("Item 2.2", treeView1[1][1].Header);
            Assert.AreEqual("Item 3", treeView1[2].Header);
            treeView1[1].Collapse();
            Assert.AreEqual("Item 3", treeView1[2].Header);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetItemWithIntIndexerWithIndexOutOfRangeTooSmall()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1[-1];
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetItemWithIntIndexerWithIndexOutOfRangeTooBig()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1[3];
        }

        [TestMethod]
        public void TestGetSelectedItem()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            treeView1["Item 1"].Select();
            TreeViewItem selectedItem = treeView1.SelectedItem;
            Assert.AreEqual("Item 1", selectedItem.Header);
        }

        [TestMethod]
        public void TestGetSelectedItemWithNoneSelected()
        {
            TreeView treeView2 = this.window.Get<TreeView>("treeView2");
            TreeViewItem selectedItem = treeView2.SelectedItem;
            Assert.IsNull(selectedItem);
        }

        [TestMethod]
        public void TestGetItem()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1.GetItem("Item 1");
            Assert.AreEqual("Item 1", item1.Header);
            TreeViewItem item2 = treeView1.GetItem("Item 2");
            Assert.AreEqual("Item 2", item2.Header);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetItemWithNullValue()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1.GetItem(null);
        }

        [TestMethod]
        public void TestGetChildItem()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1.GetItem("Item 1");
            Assert.AreEqual("Item 1", item1.Header);
            TreeViewItem item11 = item1.GetItem("Item 1.1");
            Assert.AreEqual("Item 1.1", item11.Header);
        }

        [TestMethod]
        public void TestGetItemWithStringIndexer()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1["Item 1"];
            Assert.AreEqual("Item 1", item1.Header);
            TreeViewItem item2 = treeView1["Item 2"];
            Assert.AreEqual("Item 2", item2.Header);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetItemWithStringIndexerWithNullValue()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1[null];
        }

        [TestMethod]
        public void TestGetChildItemWithStringIndexer()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item1 = treeView1["Item 1"];
            Assert.AreEqual("Item 1", item1.Header);
            TreeViewItem item11 = item1["Item 1.1"];
            Assert.AreEqual("Item 1.1", item11.Header);
        }

        [TestMethod,ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetInexistentItem()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            treeView1.GetItem("Inexistent Item");
        }

        [TestMethod, ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetInexistentItemWithStringIndexer()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem item = treeView1["Inexistent Item"];
        }

        [TestMethod]
        public void TestGetAllItems()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            TreeViewItem[] items = treeView1.Items;
            Assert.AreEqual(3, items.Length);
            Assert.AreEqual("Item 1", items[0].Header);
            Assert.AreEqual("Item 2", items[1].Header);
            Assert.AreEqual("Item 3", items[2].Header);

            TreeViewItem[] items1 = items[0].Items;
            Assert.AreEqual(1, items1.Length);
            Assert.AreEqual("Item 1.1", items1[0].Header);
        }

        [TestMethod]
        public void TestIsSelected()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            Assert.IsTrue(treeView1["Item 1"].IsSelected);
            Assert.IsFalse(treeView1["Item 2"].IsSelected);
            Assert.IsFalse(treeView1["Item 3"].IsSelected);

            treeView1["Item 2"].IsSelected = true;
            Assert.IsTrue(treeView1["Item 2"].IsSelected);
            Assert.IsFalse(treeView1["Item 1"].IsSelected);
            Assert.IsFalse(treeView1["Item 3"].IsSelected);

            treeView1["Item 2"].IsSelected = false;
            Assert.IsFalse(treeView1["Item 2"].IsSelected);
            Assert.IsFalse(treeView1["Item 1"].IsSelected);
            Assert.IsFalse(treeView1["Item 3"].IsSelected);
        }

        [TestMethod]
        public void TestSelectUnselect()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            Assert.IsTrue(treeView1["Item 1"].IsSelected);
            Assert.IsFalse(treeView1["Item 2"].IsSelected);
            Assert.IsFalse(treeView1["Item 3"].IsSelected);

            treeView1["Item 2"].Select();
            Assert.IsTrue(treeView1["Item 2"].IsSelected);
            Assert.IsFalse(treeView1["Item 1"].IsSelected);
            Assert.IsFalse(treeView1["Item 3"].IsSelected);

            treeView1["Item 1"].Select();
            Assert.IsTrue(treeView1["Item 1"].IsSelected);
            Assert.IsFalse(treeView1["Item 2"].IsSelected);
            Assert.IsFalse(treeView1["Item 3"].IsSelected);

            treeView1["Item 1"].Unselect();
            Assert.IsFalse(treeView1["Item 1"].IsSelected);
            Assert.IsFalse(treeView1["Item 2"].IsSelected);
            Assert.IsFalse(treeView1["Item 3"].IsSelected);
        }

        [TestMethod,ExpectedException(typeof(OperationNotSupportedException))]
        public void TestSelectUnselectDisabledItem()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            treeView1["Item 3"].Select();
        }

        [TestMethod]
        public void TestExpandCollapse()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            Assert.IsFalse(treeView1["Item 1"].IsExpanded);
            treeView1["Item 1"].Expand();
            Assert.IsTrue(treeView1["Item 1"].IsExpanded);
            treeView1["Item 1"].Collapse();
            Assert.IsFalse(treeView1["Item 1"].IsExpanded);

            treeView1["Item 1"].IsExpanded = true;
            Assert.IsTrue(treeView1["Item 1"].IsExpanded);
            treeView1["Item 1"].IsExpanded = false;
            Assert.IsFalse(treeView1["Item 1"].IsExpanded);
        }

        [TestMethod, ExpectedException(typeof(OperationNotSupportedException))]
        public void TestExpandDisabledItem()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            treeView1["Item 3"].Expand();
        }

        [TestMethod, ExpectedException(typeof(OperationNotSupportedException))]
        public void TestCollapseDisabledItem()
        {
            TreeView treeView1 = this.window.Get<TreeView>("treeView1");
            treeView1["Item 3"].Collapse();
        }

        protected override Type TestWindowType()
        {
            return typeof (TreeViewTestWindow);
        }

        protected override string TestWindowName()
        {
            return "TreeViewTestWindow";
        }
    }
}
